package calendar;

import javax.swing.* ;
import java.text.DecimalFormat ;
import java.util.Calendar;
import java.util.TimeZone;

/* time object used for the start and end dates/times of the calendar */
public class CalTime extends JPanel
{
  private static final long serialVersionUID = 3420595734L;

  Calendar calendar ;
  double totalSeconds = 0.0;
  DecimalFormat hourFormat ;
  DecimalFormat minuteFormat ;
  DecimalFormat secondsFormat ;
  String [] monthList =
  {
    "Jan", "Feb", "Mar", "Apr", "May", "Jun",
    "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  } ;

  private void init ()
  {
    calendar = Calendar.getInstance (TimeZone.getTimeZone("GMT")) ;

    hourFormat = new DecimalFormat () ;
    hourFormat.setMaximumIntegerDigits ( 2 ) ;
    hourFormat.setMinimumIntegerDigits ( 2 ) ;
    hourFormat.setMaximumFractionDigits ( 0 ) ;
    hourFormat.setMinimumFractionDigits ( 0 ) ;

    minuteFormat = new DecimalFormat () ;
    minuteFormat.setMaximumIntegerDigits ( 2 ) ;
    minuteFormat.setMinimumIntegerDigits ( 2 ) ;
    minuteFormat.setMaximumFractionDigits ( 0 ) ;
    minuteFormat.setMinimumFractionDigits ( 0 ) ;

    secondsFormat = new DecimalFormat () ;
    secondsFormat.setMaximumIntegerDigits ( 2 ) ;
    secondsFormat.setMinimumIntegerDigits ( 2 ) ;
    secondsFormat.setMaximumFractionDigits ( 3 ) ;
    secondsFormat.setMinimumFractionDigits ( 3 ) ;
  }

  public String toString ()
  {
    int year     = getYear () ;
    int month    = getMonth () ;
    int day      = getDay () ;
    int hour     = getHour () ;
    int minute    = getMinute () ;
    double seconds = getSeconds () ;

    String hourStr = hourFormat.format(hour) ;
    String minuteStr = minuteFormat.format(minute) ;
    String secondsStr = secondsFormat.format(seconds) ;

    String retVal = monthList[month] + " " + day  + ", " + year + " " +
               hourStr + ":" + minuteStr + ":" + secondsStr ;

    return retVal ;
  }

  public int getYear ()
  {
    int year = calendar.get ( Calendar.YEAR ) ;
    return year ;
  }

  public int getMonth ()
  {
    int month = calendar.get ( Calendar.MONTH ) ;
    return month ;
  }

  public int getDay ()
  {
    int day = calendar.get ( Calendar.DAY_OF_MONTH ) ;
    return day ;
  }

  public int getHour ()
  {
    int hour = calendar.get ( Calendar.HOUR_OF_DAY ) ;
    return hour ;
  }

  public int getMinute ()
  {
    int minute = calendar.get ( Calendar.MINUTE ) ;
    return minute ;
  }

  public double getSeconds ()
  {
    double fracSeconds = totalSeconds - Math.floor ( totalSeconds ) ;
    double seconds = (double)calendar.get( Calendar.SECOND ) + fracSeconds ;

    return seconds ;
  }

  public CalTime ()
  {
    init () ;
    totalSeconds = calendar.getTimeInMillis () / 1000.0 ;
  }

  public CalTime ( double seconds )
  {
    init () ;
    setTime ( seconds ) ;
  }

  public void setTime ( double seconds )
  {
    totalSeconds = seconds ;

    long milliSeconds = (long) Math.floor( seconds * 1000.0 );

    calendar.clear () ;
    calendar.setTimeInMillis ( milliSeconds ) ;
  }

  public void setTime ( int year,
                        int month,
                        int day,
                        int hour,
                        int minute,
                        double seconds )
  {
    calendar.set ( year, month, day, hour, minute, 0 ) ;
    totalSeconds = Math.floor(calendar.getTimeInMillis() / 1000.0) + seconds;
  }

  public double getTime ()
  {
    return totalSeconds ;
  }
}
